//
//  BRIndustryPopView.m
//  Kexu
//
//  Created by IOS on 2023/8/3.
//

#import "BRIndustryPopView.h"
@interface BRIndustryPopView ()<QMUITableViewDelegate,QMUITableViewDataSource>
{
    CGFloat _contH;
}

@end

@implementation BRIndustryPopView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.dataArr = [NSMutableArray array];
        [self setUI];
    }
    return self;
}

#pragma mark - net --
- (void)requestList {
    
    [[BRRequestManager shared] GET:[BRAPI indutstry] params:@{} succeed:^(NSDictionary * _Nonnull dataDict, NSInteger respCode) {
        
        NSArray *arr = [BRIndustryModel mj_objectArrayWithKeyValuesArray:dataDict[@"data"]];
        self.dataArr = [NSMutableArray arrayWithArray:arr];
        
        [self.tableView reloadData];
        
    } failed:^(NSInteger code, NSString * _Nonnull errMsg) {
        
    }];
}

- (void)hidd  {
    [self endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self->_contView.transform = CGAffineTransformMakeRotation(0);
    } completion:^(BOOL finished) {
        self.hidden =  YES;
    }];
}

- (void)show {
    self.hidden = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self->_contView.transform = CGAffineTransformMakeTranslation(0, -(self->_contH - 10));
    }];
}


#pragma mark - delete --
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 56;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BRIndustryListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BRIndustryListCell"];
    
    BRIndustryModel *dto = self.dataArr[indexPath.row];
    NSString *lanKey = [[[BRLanguageManager share] requestStr] lowercaseString];
    
    NSString *title = [dto.industryName valueForKey:lanKey];
    cell.titleL.text = title.length == 0 ? @"--" : title;
    NSString *text = [dto.descriptionDic valueForKey:lanKey];
    cell.textL.text = text;
    cell.textL.hidden = text.length == 0;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BRIndustryModel *dto = self.dataArr[indexPath.row];
    NSString *lanKey = [[[BRLanguageManager share] requestStr] lowercaseString];
    NSString *title = [dto.industryName valueForKey:lanKey];
    NSString *text = [dto.descriptionDic valueForKey:lanKey];
//    if (text.length != 0) {
//        title = [NSString stringWithFormat:@"%@(%@)",title,text];
//    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    dic[@"text"] = title;
    dic[@"textSub"] = text;
    dic[@"industryId"] = dto.industryId;
    dic[@"title"] = Localized(@"shop_industryCategory");
    
    if (self.selItemBlock) {
        self.selItemBlock(dic);
    }
    [self hidd];
}

#pragma mark - UI --
- (void)setUI {
    self.backgroundColor = kColorShadow(0.2);
    self.hidden =  YES;
    _contH = kSafeAreaBottomHeight + 614 + 10;
    self.contView = [[UIView alloc] init];
    self.contView.layer.cornerRadius = 10;
    self.contView.layer.masksToBounds = YES;
    self.contView.backgroundColor = UIColor.whiteColor;
    [self addSubview:self.contView];
    [self.contView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(self.mas_bottom).offset(0);
        make.height.mas_equalTo(_contH);
    }];
    
    [self.contView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(44);
        make.bottom.offset(-10);
    }];
    [self.tableView registerClass:[BRIndustryListCell class] forCellReuseIdentifier:@"BRIndustryListCell"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    QMUIButton *backBtn = [[QMUIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 58, 0, 44, 44)];
    [backBtn setBackgroundImage:UIImageMake(@"arrow_close_gray44") forState:UIControlStateNormal];
    [_contView addSubview:backBtn];
//    _closeBtn = backBtn;
    [backBtn addTarget:self action:@selector(hidd) forControlEvents:UIControlEventTouchUpInside];
}


- (QMUITableView *)tableView{
    if (!_tableView) {
        _tableView = [[QMUITableView alloc]initWithFrame:CGRectMake(0,kSafeAreaTopHeight, kScreenWidth, kScreenHeight - kSafeAreaTopHeight) style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }

        if (@available(iOS 15.0, *)) {
            [UITableView appearance].sectionHeaderTopPadding = 0;
        }
        if (@available(iOS 11.0, *)) {
            self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _tableView;
}

@end

@implementation BRIndustryListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUI];
    }
    return self;
}

#pragma mark - UI --
- (void)setUI {
    self.contentView.backgroundColor = UIColor.whiteColor;
    self.line = [BRToolsView getLineView];
    [self.contentView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-16);
        make.left.offset(16);
        make.bottom.offset(0);
        make.height.mas_equalTo(0.5);
    }];
    
    UIStackView *stack = [[UIStackView alloc] init];
    stack.spacing = 4;
    stack.axis = UILayoutConstraintAxisVertical;
    [self.contentView addSubview:stack];
    [stack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.centerY.offset(0);
        make.right.offset(-16);
    }];
    
    self.titleL = [BRToolsView br_label:@"" textColor:kColorTitle font:[UIFont fontS:15]];
    self.textL = [BRToolsView br_label:@"" textColor:kColorTitle66B font:[UIFont fontM:13]];

    [stack addArrangedSubview:self.titleL];
    [stack addArrangedSubview:self.textL];
}

@end

@implementation BRIndustryModel

+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{@"industryId":@"id",@"descriptionDic":@"description"};
}

@end
